En esta sección se muestra la estructura de la gramática actualmente soportada por el proyecto. 

Es una gramática escrita para GNU Bison \cite{bison} en la que por motivos de espacio y para facilitar la comprensión se han eliminado los fragmentos de código relacionados con cada rama de \textit{parsing} así como la definición de tipos de cada estructura. El código completo del parser y del analizador léxico puede ser consultado en los archivos SQL.y y SQL.l en la siguiente carpeta del repositorio del código: http://stcg.googlecode.com/svn/trunk/src/SQL\_analysis/SQL\_parser/ .


La gramática, expresada en la sintaxis parecida a BNF que utilizan Yacc y Bison \cite{bison} y basada en la gramática propuesta en \cite{sql92} es la siguiente:

Notese que los \textit{tokens} de la gramática están escritos en mayúsculas.

\begin{verbatim}
%start start

start:
	/*empty*/
	table_definition
	|start SC table_definition
	|view_definition
	|start SC
	;

view_definition: 
	create_view table_name vcl AS query_expression
	;

create_view: 
	CREATE VIEW
	;

vcl:
	/*empty*/
	|LP view_column_list RP
	;

view_column_list:
	column_name_list
	;

query_expression:
	non_join_query_expression
	;

non_join_query_expression:
	non_join_query_term
	|query_expression UNIONOP query_term /*UNION*/
	|query_expression INTERSECTOP query_term /*INTERSECTION*/
	;

non_join_query_term:
	non_join_query_primary
	;

query_term:
	non_join_query_term
	;

non_join_query_primary:
	simple_table
	;

simple_table:
	query_specification
	|table_value_constructor
	;

table_value_constructor: 
	VALUES table_value_constructor_list
	;

table_value_constructor_list:
	row_value_constructor
	|table_value_constructor_list COMMA row_value_constructor
	;

query_specification:
	SELECT select_list from_clause wc gbc hc  
//meaning wc:[ <where clause> ], gbc:[ <group by clause> ] hc[ <having clause> ]
	;

table_expression:
	from_clause wc gbc hc 
//meaning wc:[ <where clause> ], gbc:[ <group by clause> ] hc[ <having clause> ]
	;

from_clause :
	FROM table_reference_list
	;

table_reference :
	table_name
	|table_name correlation_specification
	|table_name AS correlation_specification
	;

correlation_specification :
	 correlation_name
	;

table_reference_list:
	table_reference
	|table_reference_list COMMA table_reference
	;

wc:
	/*empty*/
	| WHERE search_condition
	;

gbc:
	//empty
	|GROUP BY grouping_column_reference_list
	;

hc:
	//empty
	|HAVING search_condition
	;

grouping_column_reference_list:
	grouping_column_reference
	|grouping_column_reference COMMA grouping_column_reference_list
	;

grouping_column_reference :
	column_reference
	;

select_list :
	ASTERISK
	|select_list2
	;

select_list2:
	select_sublist
	|select_list2 COMMA select_sublist
	;

select_sublist:
	derived_column
	| qualifier PERIOD ASTERISK
	;

derived_column :
	value_expression
	|value_expression as_clause
	;

as_clause :
	column_name
	|AS column_name
	;

value_expression:
	numeric_value_expression
	;

numeric_value_expression :
	term
	|   numeric_value_expression PLUS term
	|   numeric_value_expression MINUS term
	;

term :
	factor
	|   term ASTERISK factor
	|   term SOLIDUS factor
	;

factor :
	numeric_primary
	|sign numeric_primary
	;

sign :
	PLUS
	|MINUS
	;

numeric_primary:
	value_expression_primary
	;

unsigned_value_specification:
	unsigned_literal
	;

unsigned_literal:
	unsigned_numeric_literal
	;

unsigned_numeric_literal:
	exact_numeric_literal
	;
exact_numeric_literal:
	unsigned_integer
	;

unsigned_integer:
	UNSIGNED_INT
	;

value_expression_primary:
	unsigned_value_specification
	| column_reference
	| LP value_expression RP
	;

column_reference:
	qualifier PERIOD column_name
	|column_name
	;

qualifier:
	table_name
	;


table_name:
	identifier
	;

column_name:
	identifier
	;

correlation_name: 
	identifier
	;

search_condition:
	boolean_term
	|search_condition OR boolean_term 
	;

boolean_term :
	boolean_factor
	|boolean_term AND boolean_factor
	;

boolean_factor:
	NOT boolean_test
	|boolean_test
	;

boolean_test :
	boolean_primary
	;

boolean_primary :
	predicate
	| LP search_condition RP
	;

predicate :
	comparison_predicate
       | exists_predicate
	;

truth_value:
	TRUET
	|FALSET
	|UNKNOWN
	;

comparison_predicate:
	 row_value_constructor comp_op row_value_constructor
	;

comp_op: //comparisson operators
	EQUALOP
	|   LTOP GTOP// meaning not equals operator <>
	|   LTOP //less than
	|   GTOP //greater than
	|   LTOP EQUALOP //less than or equal
	|   GTOP EQUALOP // greater than or equal
	;

row_value_constructor :
	row_value_constructor_element
	;

row_value_constructor_element :
	value_expression
	|   NULLT // meaning null_specification
	|   DEFAULT // meaning default_specification
	;

exists_predicate: 
	EXISTS subquery
	;

subquery :
	LP query_expression RP
	;

table_definition:
	create_table glt table_name LP table_element_list RP on_comit
	;

create_table:
	 CREATE TABLE
	;

identifier: 
	actual_identifier
	;

actual_identifier: 
	IDENTIFIER
	;

table_element_list:
	table_element
	| table_element_list COMMA table_element
	;

table_element:
	column_definition
	| table_constraint_definition
	;

table_constraint_definition:
	table_constraint
	| constraint_name_definition table_constraint
	;

table_constraint:
	unique_constraint_definition
	|referential_constraint_definition
	|check_constraint_definition
	;
	
referential_constraint_definition :
		FOREIGN KEY LP reference_column_list RP references_specification
		;

reference_column_list: 
	column_name_list
	;

references_specification:
	REFERENCES referenced_table_and_columns
	;

referenced_table_and_columns : 
	table_name
	|table_name LP reference_column_list RP
;
unique_constraint_definition:
	unique_specification LP unique_column_list RP
	;

unique_specification : UNIQUE
	| PRIMARY KEY
	;

unique_column_list : 
	column_name_list
	;

column_name_list:
	 column_name
	| column_name_list COMMA column_name
	;

constraint_name_definition:
	CONSTRAINT identifier
	;

check_constraint_definition:
	 CHECK LP search_condition RP
	;

constraint_name	:
	qualified_name
	;

qualified_name :
	 qualified_identifier
	;

qualified_identifier :
	identifier
	;

schema_name:
	catalog_name PERIOD unqualified_schema_name
	|unqualified_schema_name
	;

catalog_name:
	identifier
	;

unqualified_schema_name : 
	identifier
	;

column_definition: 
	column_name data_type
	;

data_type:
	numeric_type
	;		

numeric_type:
	exact_numeric_type
	;

exact_numeric_type:
	INTEGERT
	|INT
	;
\end{verbatim}